/*
*jquery maxLenth
*check input or textarea length
*version 0.1
*FIXME:
*
*/
(function($){
	
	jQuery.fn.maxlength = function (options){
		var defaults = {
		    maxlength:140,
		    shortUrl: false,
		    statusElement:null,
		    showStatus:true,
			statusClass:''
		};
		
		options = jQuery.extend(defaults, options);
		
		return this.each(function() 
		{
			var item = $(this);
			var Interval = null;
        item.keypress(function(event){
            var key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            if (key == 27) {
                clearInterval(Interval);
            }
            checkInputLength(item, options.maxlength);
        }).blur(function(){
            clearInterval(Interval);
            checkInputLength(item, options.maxlength);
        }).focus(function(){
            //字数监控
            clearInterval(Interval);
            Interval = setInterval(function(){
                checkInputLength( item, options.maxlength);
            }, 300);
        });
        checkInputLength($(this), options.maxlength);
		
		function getLength(str, shortUrl){
            if (true == shortUrl) {
                return Math.ceil(str.replace(/((news|telnet|nttp|file|http|ftp|https):\/\/){1}(([-A-Za-z0-9]+(\.[-A-Za-z0-9]+)*(\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\.[0-9]{1,3}){3}))(:[0-9]*)?(\/[-A-Za-z0-9_\$\.\+\!\*\(\),;:@&=\?\/~\#\%]*)*/ig, 'http://goo.gl/fkKB ').replace(/^\s+|\s+$/ig, '').replace(/[^\x00-\xff]/ig, 'xx').length /
                2);
            }
            else {
                return Math.ceil(str.replace(/[^\x00-\xff]/ig, 'xx').length / 2);
            }
        };
        
        var wordText = null;
        //检查字数输入
        function checkInputLength(obj, num){
            var len = getLength($(obj).val(), false);
            var wordNumObj = options.statusElement;
           
            if(options.showStatus){
            	if (len == 0) {
                    wordNumObj.html('<span>' + (num - len) + '</span>');
    				if(options.statusClass != ''){
    					wordNumObj.addClass(options.statusClass);
    				}
                }
                else{
                	if (len > num) {
                        $(obj).val(wordText);
                    }
                    else{
                    	if (len <= num) {
                    		 var text = $(obj).val();
                    		 wordText = text;
                        	wordNumObj.html('<span>' + (num - len) + '</span>');
    						if(options.statusClass != ''){
    							wordNumObj.addClass(options.statusClass);
    						}
                        }
                    } 
                }
            }
            else{
            	if (len > num) {
					var text = $(obj).val();
                    $(obj).val(text.substr(0,options.maxlength));
                }
            }
        };
		});
	};
})(jQuery);

//字符串长度-中文和全角符号为1，英文、数字和半角为0.5
        